package com.jinsp.router_api;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import java.util.ArrayList;

/**
 * Created by luoshuai on 12/13/21
 * <p>
 * 参数管理
 */
public class ParameterManager {

    private Bundle bundle;       //用来保存跳转参数
    private int enterAnim, exitAnim;    //过渡动画

    public ParameterManager addParameters(String name, String value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putString(name, value);
        return this;
    }

    public ParameterManager addParameters(String name, int value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putInt(name, value);
        return this;
    }

    public ParameterManager addParameters(String name, float value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putFloat(name, value);
        return this;
    }

    public ParameterManager addParameters(String name, short value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putShort(name, value);
        return this;
    }

    public ParameterManager addParameters(String name, byte value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putByte(name, value);
        return this;
    }

    public ParameterManager addParameters(String name, boolean value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putBoolean(name, value);
        return this;
    }

    public ParameterManager addParameters(String name, long value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putLong(name, value);
        return this;
    }

    public ParameterManager addParameters(String name, double value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putDouble(name, value);
        return this;
    }

    public ParameterManager addParameters(String name, Bundle bundle) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putBundle(name, bundle);
        return this;
    }

    public ParameterManager addArrayParameters(String name, String[] value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putStringArray(name, value);
        return this;
    }

    public ParameterManager addArrayParameters(String name, boolean[] value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putBooleanArray(name, value);
        return this;
    }

    public ParameterManager addArrayParameters(String name, int[] value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putIntArray(name, value);
        return this;
    }

    public ParameterManager addArrayParameters(String name, float[] value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putFloatArray(name, value);
        return this;
    }

    public ParameterManager addArrayParameters(String name, short[] value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putShortArray(name, value);
        return this;
    }

    public ParameterManager addArrayParameters(String name, long[] value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putLongArray(name, value);
        return this;
    }

    public ParameterManager addArrayParameters(String name, double[] value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putDoubleArray(name, value);
        return this;
    }

    public ParameterManager addArrayParameters(String name, byte[] value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putByteArray(name, value);
        return this;
    }

    public ParameterManager addStringListParameters(String name, ArrayList<String> value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putStringArrayList(name, value);
        return this;
    }

    public ParameterManager addIntegerListParameters(String name, ArrayList<Integer> value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putIntegerArrayList(name, value);
        return this;
    }

    public ParameterManager addBundle(String name, Bundle value) {
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putBundle(name, value);
        return this;
    }

    public ParameterManager addAll(Bundle value) {
        if (value != null) {
            if (bundle == null) {
                bundle = new Bundle();
            }
            bundle.putAll(value);
        }
        return this;
    }

    /**
     * 转场动画，Activity使用
     *
     * @param enterAnim 进入动画
     * @param exitAnim  退出动画
     * @return 参数配置实例
     */
    public ParameterManager addAnim(int enterAnim, int exitAnim) {
        this.enterAnim = enterAnim;
        this.exitAnim = exitAnim;
        return this;
    }

    protected Bundle getBundle() {
        return bundle;
    }

    protected int getEnterAnim() {
        return enterAnim;
    }

    protected int getExitAnim() {
        return exitAnim;
    }

    /**
     * 启动跳转页面
     *
     * @param context     上下文
     * @param requestCode 请求码，-1不使用ForResult
     */
    public void startActivity(Context context, int requestCode) {
        if (context == null) {
            throw new NullPointerException("Context can not be empty");
        }
        RouterApi.navigationActivity(context, this, requestCode);
    }

    /**
     * Fragment内启动跳转页面
     *
     * @param fragment    v4Fragment实例
     * @param requestCode 请求码，-1不使用ForResult
     */
    public void startActivity(Fragment fragment, int requestCode) {
        if (fragment == null) {
            throw new NullPointerException("V4Fragment can not be empty");
        }
        RouterApi.navigationActivity(fragment, this, requestCode);
    }

    /**
     * 启动服务
     *
     * @param context 上下文
     */
    public void startService(Context context) {
        if (context == null) {
            throw new NullPointerException("Context can not be empty");
        }
        RouterApi.navigationService(context, this, false);
    }

    /**
     * 关闭服务
     *
     * @param context 上下文
     */
    public void stopService(Context context) {
        if (context == null) {
            throw new NullPointerException("Context can not be empty");
        }
        RouterApi.navigationService(context, this, true);
    }

    /**
     * 获取目标Fragment
     *
     * @return v4包下Fragment实例
     */
    public Fragment getV4Fragment() {
        return RouterApi.navigationFragment(this);
    }
}
